Test Series - Data Structure

Test Number 42/115

Q: Which of the following is the predefined function for array reversal in C++?
A. rotate()
B. arr_rotate()
C. array_rotate()
D. rot()
Solution: The predefined function for rotating an array is rotate() in C++. It is defined under the library algorithm and requires 3 arguments.
Q: How many arguments are required by the predefined function rotate() in C++?
A. 1
B. 2
C. 3
D. 4
Solution: The predefined function for rotating an array is rotate() in C++ which comes under the library called an algorithm. It requires 3 arguments.
Q: Predefined function rotate() in C++ is available under which header file?
A. math
B. stdio
C. stdlib
D. algorithm
Solution: The predefined function for rotating an array is rotate() in C++ which comes under the library called algorithm. It requires 3 arguments the first being the pointer to the starting index of the array and the last being the pointer to the last index of the array. The middle argument is the pointer to the element that becomes the first element in the rotated array.
Q:  Which of the following algorithm to rotate an array has the maximum time complexity?
A. rotate elements one by one
B. juggling algorithm
C. reversal algorithm
D. using a temporary array
Solution: The maximum time complexity is required by the algorithm that rotates elements one by one. It requires O(n*d) time.
Q: What is the time complexity of the juggling algorithm to rotate an array?
A. O(1)
B. O(n)
C. O(d)
D. O(n*d)
Solution: Time complexity of juggling algorithm is O(n). Its auxiliary space complexity is O(1).
Q: Reversal algorithm and juggling algorithm for array rotation have the same time complexity.
A. True
B. False
C. ....
D. ....
Solution: Time complexity of juggling algorithm is O(n) which like that of reversal algorithm. They also have the same space complexity.
Q: What will be the resulting array after reversing arr[]={3,5,4,2}?
A. 2,3,5,4
B. 4,2,3,5
C. 5,4,2,3
D. 2,4,5,3
Solution: The resulting array upon reversing after reversal is arr[]={2,4,5,3}. We can implement an algorithm for this purpose in various possible ways.
Q: How many swaps are required for reversing an array having n elements where n is an odd number?
A. (n-1) / 2
B. n/2
C.  (n/2) – 1
D. (n+1)/2
Solution: The number of swaps required for an odd element and an even element array is different because in an odd element array the position of the middle element does not need to be changed. So the number of swaps will be (n-1) / 2.
Q: How many swaps are required for reversing an array having n elements where n is an even number?
A. (n-1) / 2
B. n/2
C. (n/2) – 1
D. (n+1)/2
Solution: The number of swaps required for an odd element and an even element array is different because in an odd element array the position of the middle element does not need to be changed. So number of swaps required will be n/2.
Q: What will be the output of the following code?

#include  
using namespace std; 
 
void func(int arr[], int left, int right) 
{     
	while (left < right) 
	{ 
		int temp = arr[left]; 
		arr[left] = arr[right]; 
		arr[right] = temp; 
		left++; 
		right--; 
	} 
 
}	 
 
void printArray(int arr[], int size) 
{ 
    for (int i = 0; i < size; i++) 
    cout << arr[i] << " "; 
} 
 
int main() 
{ 
	int arr[] = {1,4,3,5}; 
	int n = sizeof(arr) / sizeof(arr[0]); 
	func(arr, 0, n-1); 
	printArray(arr, n); 
	return 0; 
}
A. 5 1 4 3
B. 3 5 1 4
C. 5 3 4 1
D.  error
Solution: The given code reverses the input array and then prints the resulting array. So the output of the given code will be 5 3 4 1.

You Have Score    /10